package com.oristartech.netty.server.auto;

import com.oristartech.netty.common.dispatcher.ChannelSender;
import com.oristartech.netty.common.handler.NettyNoticeChannelHandler;
import com.oristartech.netty.common.handler.NettyRequestChannelHandler;
import com.oristartech.netty.common.handler.NettyResponseChannelHandler;
import com.oristartech.netty.server.config.NettyServerConfig;
import com.oristartech.netty.server.handler.NettyServerHeartbeatHandler;
import com.oristartech.netty.server.handler.NettyServerRegisterHandler;
import com.oristartech.netty.server.starter.NettyClientPool;
import com.oristartech.netty.server.starter.NettyClientSender;
import com.oristartech.netty.server.starter.NettyServerChannelInitializer;
import com.oristartech.netty.server.starter.NettyServerStarter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 ** Netty服务端自动配置
 *
 * @author: chenfenghai
 * @version: 1.0
 * @date: 2020-03-23 10:14
 * @blame Team
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(NettyServerConfig.class)
public class NettyServerAutoConfiguration {

    @Autowired
    private ApplicationContext applicationContext;

    @Bean
    @ConditionalOnMissingBean(NettyServerRegisterHandler.class)
    NettyServerRegisterHandler nettyServerRegisterHandler(){
        return new NettyServerRegisterHandler();
    }

    @Bean
    @ConditionalOnMissingBean(NettyServerHeartbeatHandler.class)
    NettyServerHeartbeatHandler nettyServerHeartbeatHandler(){
        return new NettyServerHeartbeatHandler();
    }

    @Bean
    @ConditionalOnMissingBean(NettyNoticeChannelHandler.class)
    NettyNoticeChannelHandler nettyNoticeChannelHandler(){
        return new NettyNoticeChannelHandler(applicationContext);
    }

    @Bean
    @ConditionalOnMissingBean(NettyRequestChannelHandler.class)
    NettyRequestChannelHandler nettyRequestChannelHandler(){
        return new NettyRequestChannelHandler(applicationContext);
    }

    @Bean
    @ConditionalOnMissingBean(NettyResponseChannelHandler.class)
    NettyResponseChannelHandler nettyResponseChannelHandler(){
        return new NettyResponseChannelHandler();
    }

    @Bean
    @ConditionalOnMissingBean(ChannelSender.class)
    ChannelSender channelSender(){
        return new ChannelSender();
    }

    @Bean
    @ConditionalOnMissingBean(NettyClientPool.class)
    NettyClientPool nettyClientPool(){
        return new NettyClientPool();
    }

    @Bean
    @ConditionalOnMissingBean(NettyClientSender.class)
    NettyClientSender nettyClientSender(){
        return new NettyClientSender();
    }

    @Bean
    @ConditionalOnMissingBean(NettyServerChannelInitializer.class)
    NettyServerChannelInitializer nettyServerChannelInitializer(){
        return new NettyServerChannelInitializer();
    }

    @Bean
    @ConditionalOnMissingBean(NettyServerStarter.class)
    NettyServerStarter nettyServerStarter(){
        return new NettyServerStarter();
    }
}
